package com.core.reach.cloudhub.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.core.reach.cloudhub.domain.CommonResult;
import com.core.reach.cloudhub.domain.Payment;
import com.core.reach.cloudhub.handler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ga.zhang
 * @version V1.0
 * @description: TODO
 * @date 2020/3/31 11:41
 **/
@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "exceptionHandler")
    public CommonResult<Payment> byResource() {
        return new CommonResult<>(200, "按资源名称限流测试成功", new Payment(2020L, "serial001"));
    }

    public CommonResult exceptionHandler(BlockException be) {
        return new CommonResult(444, be.getClass().getCanonicalName() + "\t 服务不可用");
    }

    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult<Payment> byUrl() {
        return new CommonResult<>(200, "按Url限流测试成功", new Payment(2020L, "serial002"));
    }

    @GetMapping("/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class, blockHandler = "exceptionHandler2")
    public CommonResult<Payment> customerBlockHandler() {
        return new CommonResult<>(200, "按用户自定义处理", new Payment(2020L, "serial003"));
    }

}
